let ActicleId;
layui.config({
	version:'1510164124181'
}).use(['form', 'laypage', 'layer', 'element', 'upload'],function(){
	form = layui.form;
	layer = layui.layer;
	$ = layui.jquery;
	upload = layui.upload;//涓婁紶
    laypage = layui.laypage;//分页
    element = layui.element; //元素操作
	ActicleId=GetQueryString("ActicleId");
	TypelistByTypeId(1);//查询所有文章分类
	if(!isNotNull(ActicleId)){
		ActicleId=0;
		$("#title_name").html("添加文章");
	}else{
		state=1;
		$("#title_name").html("修改文章");
		SelectActicleListById();
	}


    let uploadInst = upload.render({
		elem: '#uploadimgs'
		,url: httpurl+'/uploadImgQiniu?pathName=acticle&divisor=7&dividend=3'
		,before: function(obj){
		  //预读本地文件示例，不支持ie8
		  obj.preview(function(index, file, result){
			$("#divpreview").css("display","block");
			$('#adv_preview').attr('src', result); //图片链接（base64）
		  });
		}
		,done: function(res){
			if(res.rtnCode==500){//如果上传失败
				$("#divpreview").css("display","none");
				$('#preview').attr('src', ""); //图片链接（base64）
				return layer.msg(res.msg);
			}
			$('#adv_preview').attr('name',res.msg);
		}
		,error: function(){
		  //演示失败状态，并实现重传
		  var demoText = $('#demoText');
		  demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn  demo-reload">重试</a>');
		  demoText.find('.demo-reload').on('click', function(){
			uploadInst.upload();
		  });
		}
	 });
});

function SelectActicleListById(){
	$.ajax({
		url: httpurl+"/findHLId",
		cache: false,
		headers: {
			"Access-Control-Allow-Origin": "*",
			"Access-Control-Allow-Methods": "*",
			"Access-Control-Allow-Headers": "Authorization",
			"Authorization": GetTokenInfo()
		},
		contentType : 'application/json',
		data:{
			"id":ActicleId,
		},
		success: function(data) {
			let dataInfo=data.data;
			$("#title").val(dataInfo.title);//文章标题
			$("#autor").val(dataInfo.autor);
			$("#show_info input[name='show_info'][value='"+dataInfo.istop+"']").attr("checked",true);//是否置顶
			$("#types").find("option[value='"+dataInfo.typelist.id+"']").attr("selected",true);
			if(isNotNull(dataInfo.mastmap)){$("#divpreview").show()};
			$("#adv_preview").attr("src",visitimgurl+'/'+dataInfo.mastmap);
			$('#adv_preview').attr('name',dataInfo.mastmap);

			if(isNotNull(dataInfo.abstracts)){
			UE.getEditor('container1').setContent(dataInfo.abstracts)};

			if(isNotNull(dataInfo.describe)){
			UE.getEditor('container2').setContent(dataInfo.describe)};

			if(isNotNull(dataInfo.detail)){
			UE.getEditor('container3').setContent(dataInfo.detail)};
		}
	})
}
function insertHeadLines(){
	/*常规*/
	let title_info=$("#title").val();//文章标题
	let is_top=$("input[name='show_info']:checked").val();
	let choice=$("input[name='choice']:checked").val();

	let ClassifyId=$("#types option:selected").val();
	let headimg=$("#adv_preview").attr("name");
	let container1=UE.getEditor('container1').getContent();  //摘要
	let container2=UE.getEditor('container2').getContent();  //文章摘要
	let container3=UE.getEditor('container3').getContent();  //详情

	let autor=$("#autor").val();
	if(!isNotNull(title_info)){
		top.layer.msg("请输入文章标题!");
		return;
	}
	if(ClassifyId==0){
		top.layer.msg("请选择文章分类!");
		return;
	}
	if(!isNotNull(container1)){
		top.layer.msg("请添加文章摘要!");
		return;
	}
	if(!isNotNull(container2)){
		top.layer.msg("请添加文章描述!");
		return;
	}
	if(!isNotNull(container3)){
		top.layer.msg("请添加详情!");
		return;
	}

	top.layer.confirm('是否确定提交？',{icon: 3,title:'提示信息'},function(index){
		var index = top.layer.msg('提交中，请稍候',{icon: 16,time:false,shade:0.8});
		let tempHttp="updateHeadLines";
		if(ActicleId==0){
			tempHttp="insertHeadLines";
		}
		setTimeout(function(){
			$.ajax({
				type:"POST",
				url: httpurl+"/"+tempHttp,
				cache: false,
				headers: {
					"Access-Control-Allow-Origin": "*",
					"Access-Control-Allow-Methods": "*",
					"Access-Control-Allow-Headers": "Authorization",
					"Authorization": GetTokenInfo()
				},
				contentType : 'application/json',
				data: JSON.stringify({
					"id":ActicleId,
					"title":title_info,
					"choice":choice,
					"autor":autor,
					"istop":is_top,
					"tid":ClassifyId,
					"abstracts":container1,
					"describe":container2,
					"detail":container3,
					"mastmap":headimg,
					"choice":choice
				}),
				dataType: "json",
				success: function(data) {
					console.log(JSON.stringify(data));
					if(data.rtnCode==200){
						top.layer.closeAll();
						top.layer.msg(data.msg);
						return_list();
					}
				}
			});
		},1000);
	})
}
function return_list(){
	window.location.href="ActicleList.html";
}
